// ServerProgramDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ServerProgram.h"
#include "ServerProgramDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CServerProgramDlg dialog


CServerProgramDlg::CServerProgramDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CServerProgramDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CServerProgramDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CServerProgramDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BTNSTART, &CServerProgramDlg::OnClickedBTNStart)
	ON_BN_CLICKED(IDC_BTNSTOP, &CServerProgramDlg::OnClickedBTNStop)
END_MESSAGE_MAP()


// CServerProgramDlg message handlers

BOOL CServerProgramDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	_numSocket = 0;
	_numListenSocket = 0;
	GetDlgItem(IDC_BTNSTOP)->EnableWindow(FALSE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CServerProgramDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CServerProgramDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CServerProgramDlg::OnClickedBTNStart()
{
	//Server's working
	AfxBeginThread(WorkThread,this);
}

//void CServerProgramDlg::OnTimer()
//{
//	_numListenSocket++;
//	LPDWORD iExitCode = new DWORD();
//	GetExitCodeThread(_recieveThread->m_hThread,iExitCode);
//	TerminateThread(_recieveThread->m_hThread,*iExitCode);
//
//	_recieveThread = AfxBeginThread(RecieveMessage,this);
//}

void CServerProgramDlg::OnClose()
{

}
void CServerProgramDlg::OnClickedBTNStop()
{
	GetDlgItem(IDC_BTNSTOP)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTNSTART)->EnableWindow(TRUE);

	CMessage iMes;
	iMes.SetCode('5');
	iMes.SetUser("");
	iMes.SetMes("");

	char* buf = new char[300];
	iMes.ToChar(buf);

	for (int i = 0; i<_connectingSocketArray.Size();i++)
	{
		send(_connectingSocketArray.GetSocket(i),buf,strlen(buf),0);
	}

	LPDWORD iExitCode = new DWORD();
	GetExitCodeThread(_acceptThread->m_hThread,iExitCode);
	TerminateThread(_acceptThread->m_hThread,*iExitCode);
	for (int i = 0;i<_threadArray.size();i++)
	{
		GetExitCodeThread(_threadArray[i]->m_hThread,iExitCode);
		TerminateThread(_threadArray[i]->m_hThread,*iExitCode);
	}

	shutdown(_listenSocket,SD_BOTH);
	closesocket(_listenSocket);
	OnCancel();
}